/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rmi;

import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author thomas
 */
public class Chaine extends UnicastRemoteObject implements ChaineInterface {

    private List<Hotel> hotels = new ArrayList();

    public Chaine(String... args) throws RemoteException {
        /* récupération des hôtels de la chaîne dans le fichier xml passé en 1er argument */
        DocumentBuilder docBuilder = null;
        Document doc = null;
        try {
            docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            doc = docBuilder.parse(new File(args[0]));

            String name, localisation;
            NodeList list = doc.getElementsByTagName("Hotel");
            NamedNodeMap attrs;
            /* acquisition de toutes les entrées de la base d'hôtels */
            for (int i = 0; i < list.getLength(); i++) {
                attrs = list.item(i).getAttributes();
                name = attrs.getNamedItem("name").getNodeValue();
                localisation = attrs.getNamedItem("localisation").getNodeValue();
                Hotel Test = new Hotel(name, localisation);
                hotels.add(Test);
//                System.out.println("ajout : " + Test.toString());
            }
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            Logger.getLogger(Chaine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public List<Hotel> getListHotel(String localisation) throws RemoteException {

        List<Hotel> hotelsLocate = new ArrayList();
        for (Hotel h : hotels) {
            if (h.localisation.equals(localisation)) {
                hotelsLocate.add(h);
            }
        }

        return hotelsLocate;
    }
}
